package com.xuexisheqv.oss.controller;

import com.xuexisheqv.oss.entity.Result;

import com.xuexisheqv.oss.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class FileController {
    @Resource
    private FileService fileService;

    @Value("${storage.service.type}")
    private String storageType;


    @RequestMapping("/testGetAllBuckets")
    public String testGetAllBuckets() throws Exception {
        List<String> allBuckets = fileService.getAllBucket();
        return allBuckets.get(0);
    }

    @RequestMapping("/getUrl")
    public String getUrl(String bucketName, String objectName) throws Exception {
        return fileService.getUrl(bucketName, objectName);
    }

    /**
     * 上传文件
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile uploadFile, String bucket, String objectName) throws Exception {
        if (log.isInfoEnabled()){
            log.info("oss-upload：bucket:{},objectName:{}",bucket,objectName);
        }
        String url = fileService.uploadFile(uploadFile, bucket, objectName);
        return Result.ok(url);
    }

}
